package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

/**
 * A JPanel encapsulating an image.
 * @author Per P
 */
public class ImagePanel extends JPanel implements MouseListener {

    private Image image = null;
    private boolean hovered = false;
    private boolean hoverSensitive = false;
    private final Color hoverColor = new Color(255, 255, 255, 64);

    ImagePanel() {
    }

    void setImage(Image image) {
        this.image = image;
        Dimension d = new Dimension(image.getWidth(null), image.getHeight(null));
        setSize(d);
        setPreferredSize(d);
    }

    public ImagePanel(Image image) {
        this.image = image;
        Dimension d = new Dimension(image.getWidth(null), image.getHeight(null));
        setSize(d);
        setPreferredSize(d);

        addMouseListener(this);
    }

    public void setHoverSensitive(boolean b) {
        hoverSensitive = b;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
        if (hovered) {
            g.setColor(hoverColor);
            g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        if (hoverSensitive) {
            hovered = true;
            repaint();
        }
    }

    public void mouseExited(MouseEvent e) {
        if (hoverSensitive) {
            hovered = false;
            repaint();
        }
    }
}